﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using JiaBao.Core.Infrastructure;
using JiaBao.Plugin.Import;
using JiaBao.Services.LogService;
using JiaBao.Services.WPFFormPlug;
using JiaBao.WPF.Customer;
using JiaBao.WPF.Framework;
using JiaBao.WPF.Setting;

namespace JiaBao.WPF
{

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : IRegistrarWPFForm
    {

        private readonly IWPFFormService _wPFFormService;

        #region Ctor

        public MainWindow(IWPFFormService wPFFormService)
        {
            InitializeComponent();

            _wPFFormService = wPFFormService;
            buttonWord.Text = "";
            //加载插件
            LoadPlugins();
            MenuIni();
        }

        /// <summary>
        /// 窗体启动时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_ContentRendered(object sender, EventArgs e)
        {
            try
            {
                DKPL.Children[0].RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }
        }
 

        /// <summary>
        /// 窗体变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            try
            {
                if (null != g_Left.Tag)
                    ((Button)g_Left.Tag).RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                if (AppStart.type != 2)
                    if (MessageBox.Show("确认退出系统", "", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                    {
                        e.Cancel = true;
                        return;
                    }
            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }

        }

        /// <summary>
        /// 重写退出程序
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            App.Current.Shutdown();
        }

        #endregion

        #region 主页方法


        /// <summary>
        /// 初始化插件
        /// </summary>
        private void LoadPlugins()
        {
            try
            {
                var plugins = _wPFFormService.LoadAllForms();

                foreach (var plugin in plugins)
                {
                    var b = new Button();
                    b.Content = plugin.GetDisplayMenuName()[0];
                    b.Margin = new Thickness(2);
                    b.Height = 120;

                    var c = plugin.GetMainWindow();

                    if (c is Window)

                        b.Click += (object sender, RoutedEventArgs e) =>
                        {
                            var w = plugin.GetMainWindow() as Window;
                            w.WindowState = WindowState.Maximized;
                            w.Show();
                        };

                    else if (c is Page)
                        b.Click += (object sender, RoutedEventArgs e) => AddNewItem((Page)plugin.GetMainWindow(), b.Content.ToString());

                    else
                    {
                        //其他
                    }
                    G_Plugins.Children.Add(b);
                }
            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }
        }


        #region 菜单项

        /// <summary>
        /// 初始化菜单
        /// </summary>
        private void MenuIni()
        {
            try
            {
                Dictionary<Button, DockPanel> kv = new Dictionary<Button, DockPanel>();

                Button b = null;
                foreach (var c in DKPL.Children)
                {

                    if (c.GetType() == typeof(DockPanel))
                    {
                        var d = (DockPanel)c;

                        b.Click += (object sender, RoutedEventArgs e) =>
                            {

                                g_Left.Tag = sender;
                                foreach (var c1 in DKPL.Children)
                                    if (c1.GetType() == typeof(DockPanel))
                                        ScaleEasingAnimationShow((DockPanel)c1, ((DockPanel)c1).Height, 0);

                                var h = g_Left.ActualHeight - (b.ActualHeight * (DKPL.Children.Count / 2));
                                ScaleEasingAnimationShow((DockPanel)d, 0, h);

                            };
                    }
                    else
                    {
                        b = (Button)c;
                    }

                }
            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 动画效果
        /// </summary>
        /// <param name="element"></param>
        /// <param name="sizeFrom"></param>
        /// <param name="sizeTo"></param>
        private void ScaleEasingAnimationShow(UIElement element, double sizeFrom, double sizeTo)
        {
            try
            {
                if (null == element) return;
                if (Double.IsNaN(sizeFrom)) sizeFrom = 0;
                if (double.IsNaN(sizeTo)) sizeTo = 0;

                var widthAnimation = new DoubleAnimation()
                {
                    From = sizeFrom,
                    To = sizeTo,
                    Duration = TimeSpan.FromSeconds(0.1),
                    RepeatBehavior = new RepeatBehavior(1),

                };
                element.BeginAnimation(HeightProperty, widthAnimation);
            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }
        }


        #endregion

        #region Tab

        #region 添加

        /// <summary>
        /// 打开页面
        /// </summary>
        /// <param name="content"></param>
        /// <param name="tabname"></param>
        private void AddNewItem<T>(string tabname) where T : class
        {
            //如果以打开退出
            if (CheckOpened(tabname))
                return;

            AddTab(EngineContext.Current.Resolve<T>() as Page, tabname);
        }

        private void AddNewItem(Page control, string tabname)
        {
            //如果以打开退出
            if (CheckOpened(tabname))
                return;

            AddTab(control, tabname);
        }

        /// <summary>
        /// 检测是否打开
        /// </summary>
        /// <param name="tabname"></param>
        /// <returns></returns>
        private bool CheckOpened(string tabname)
        {
            try
            {
                foreach (TabItem item in tab_Main.Items)
                {
                    if (item.Header.ToString() == tabname)
                    {
                        tab_Main.SelectedItem = item;
                        return true;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {

                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
                return false;
            }




        }

        /// <summary>
        /// 添加标签
        /// </summary>
        /// <param name="control"></param>
        /// <param name="tabname"></param>
        private void AddTab(Page control, string tabname)
        {
            try
            {
                //添加标签
                TabItem tnew = new TabItem();
                tnew.Height = 30;
                tnew.IsTabStop = false;
                var f = new Frame();
                f.Content = control;
                tnew.Content = f;
                tnew.Header = tabname;

                tab_Main.Items.Add(tnew);

                tab_Main.SelectedIndex = tab_Main.Items.Count - 1;
            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }
        }

        #endregion



        #endregion

        #endregion

        #region 菜单

        /// <summary>
        /// 数据库设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void M_ServerSetting_Click(object sender, RoutedEventArgs e)
        {
            EngineContext.Current.Resolve<W_ServerSetting>().ShowDialog();
        }

        #endregion

        #region 按钮事件


        private void btn_D_1_1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var c = EngineContext.Current.ContainerManager.Resolve<p_Customer_Saler>();
                c.CustomerType = CustomerType.Jtdj;
                AddNewItem(c, ((Button)sender).Content.ToString());
            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }
        }


        private void btn_D_1_2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var c = EngineContext.Current.ContainerManager.Resolve<p_Customer_Saler>();
                c.CustomerType = CustomerType.Saler;
                AddNewItem(c, ((Button)sender).Content.ToString());
            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }

            //AddNewItem<p_Customer_Saler>(((Button)sender).Content.ToString());
        }

        private void btn_D_1_3_Click(object sender, RoutedEventArgs e)
        {
            AddNewItem<p_Customer>(((Button)sender).Content.ToString());

        }

        private void btn_D_1_4_Click(object sender, RoutedEventArgs e)
        {
            AddNewItem<p_Customer_Excel>(((Button)sender).Content.ToString());
        } 
        private void btn_D_1_5_Click(object sender, RoutedEventArgs e)
        {
            AddNewItem<p_Customer_Birthday>(((Button)sender).Content.ToString());
        }




        #endregion

        private void btn_Import_Click(object sender, RoutedEventArgs e)
        {
            p_Import b = new p_Import();
            AddTab(b, "数据导入");
        }
    }


}
